#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>

// 1读取,2，写入
int main(int argc, char *argv[])
{
    int fd, ret = 0;
    char readbuf[50];
    char *writebuf = "I am user";
    if (argc != 3)
    {
        printf("Usage: <./app> /dev/dev* 1/2\r\n");
        return -1;
    }
    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        printf("open %s error\r\n", argv[1]);
        return -1;
    }

    if (atoi(argv[2]) == 1)
    {
        memset(readbuf, 0, 50);
        ret = read(fd, readbuf, 50);
        if (ret < 0)
        {
            printf("read kernel data error\r\n");
            return ret;
        }
        else
        {
            printf("read kernel data:%s\r\n", readbuf);
        }
    }
    if (atoi(argv[2]) == 2)
    {
        ret = write(fd, writebuf, strlen(writebuf));
        if (ret < 0)
        {
            printf("write kernel data error\r\n");
            return ret;
        }
    }

    sleep(1);
    // 关闭设备文件
    close(fd);

    return 0;
}